<script src="common.js"></script>
<script>

if(!("duration" in localStorage)) {
	localStorage.duration = 10;
}

function update()
{
	var xhr = new XMLHttpRequest();
	xhr.open('GET', url, true);
	xhr.onload = function() {
		var text = xhr.responseText;
		var badge = getNewArticleBadge(text);
		chrome.browserAction.setBadgeText({text:badge});
	};
	xhr.send(null);
	timer = localStorage.duration*60;
}

var timer = 0;
function eventLoop()
{
	var t = setTimeout("eventLoop()", 1000);
	timer--;
	if(timer < 0) {
		update();
	}
}

eventLoop();

function onRequest(request, sender, callback) {
	if (request.action == 'update') {
		update();
	}
};

// Wire up the listener.
chrome.extension.onRequest.addListener(onRequest);

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
	if(tab.url.indexOf(domain) != -1) {
		update();
	}
});
</script>
